import 'package:flutter/material.dart';

/// FileName test_animated_list
///
/// @Author lax
/// @Date 2024/6/4
///
/// @Description

class TestAnimatedList extends StatefulWidget {
  const TestAnimatedList({super.key});

  @override
  State<StatefulWidget> createState() {
    return _TestAnimatedListState();
  }
}

class _TestAnimatedListState extends State<TestAnimatedList> {
  final GlobalKey<AnimatedListState> globalKey = GlobalKey();

  var data = "ABCDEFG".split("");

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: AnimatedList(
        key: globalKey,
        padding: const EdgeInsets.all(0),
        initialItemCount: data.length,
        itemBuilder: (context, index, animation) {
          return FadeTransition(
            opacity: animation,
            child: ListTile(
              title: Text(data[index], textScaleFactor: 2),
              trailing: GestureDetector(
                onTap: () {
                  setState(() {
                    data.removeAt(index);
                    globalKey.currentState?.removeItem(index,
                        (context, animation) {
                      return const SizedBox();
                    });
                  });
                },
                child: const Icon(Icons.delete),
              ),
            ),
          );
        },
      ),
      floatingActionButton: FloatingActionButton(
        child: const Icon(Icons.add),
        onPressed: () {
          setState(() {
            data.insert(0, 'new');
            globalKey.currentState?.insertItem(0);
          });
        },
      ),
    );
  }
}
